package com.item.read.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.item.read.vo.BookSearchVo;
import com.item.read.vo.BookSourceRuleSearch;
import com.item.read.vo.BookSourceVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 书源管理
 */
public class BookHelper {

    public static String SEARCH_KEY = "{{key}}";

    /**
     * 书源集合
     */
    public static List<BookSourceVo> bookSourceVos = new ArrayList<>();

    static {
        String utf8Str = HttpUtils.sendHttpOfString("GET", "https://e-c.coding.net/p/yicheng/d/YD/git/raw/master/sy.json");
        bookSourceVos.addAll(JSONUtil.toList(utf8Str, BookSourceVo.class));
    }

    /**
     * 搜索书籍
     *
     * @param name
     * @return
     */
    public static void searchBook(String name, SearchBookResult searchBookResult) {
        for (BookSourceVo bookSourceVo : bookSourceVos) {
            BookSourceRuleSearch ruleSearch = bookSourceVo.getRuleSearch();
            if(ObjectUtil.isEmpty(ruleSearch)){
                continue;
            }
            // 书集合规则
            String bookListRule = ruleSearch.getBookList();
            // 书名规则
            String bookNameRule = ruleSearch.getName();
            if(ObjectUtil.isEmpty(bookNameRule)){
                continue;
            }
            String html;
            // 书源地址
            String bookSourceUrl = bookSourceVo.getBookSourceUrl();
            // 搜索地址
            String searchUrl = bookSourceVo.getSearchUrl();
            if (ObjectUtil.isEmpty(searchUrl)) {
                continue;
            }
            if (searchUrl.contains(",")) {
                String oldInfo = searchUrl;
                // 逗号分割 "searchUrl": "/search.html,{\n  'method': 'POST',\n  'body': 'searchkey={{key}}'\n}",
                searchUrl = oldInfo.substring(0, oldInfo.indexOf(","));
                String json = oldInfo.substring(oldInfo.indexOf(",") + 1, oldInfo.length());
                BookSearchVo bookSearchVo = JSONUtil.toBean(json, BookSearchVo.class);
                String body = bookSearchVo.getBody();
                body = body.replace(SEARCH_KEY, name);
                html = HttpUtils.sendHttpOfString(bookSearchVo.getMethod(), bookSourceUrl + searchUrl, body);
            } else {
                searchUrl = searchUrl.replace(SEARCH_KEY, name);
                html = HttpUtils.sendHttpOfString("GET", bookSourceUrl + searchUrl);
            }
            if(ObjectUtil.isNotEmpty(html)) {
                Element body = Jsoup.parse(html).body();
                Elements select = body.select(bookListRule);
                if(select.size() <= 0){
                    continue;
                }
                for (Element element : select) {
                    element.select("a").get(1).attr("text");
                    Elements bookNameSelect = element.select(bookNameRule);
                    if(ObjectUtil.isNotEmpty(bookNameSelect)){
                        String s = bookNameSelect.get(0).toString();
                        System.out.println(s);
                    }
                }
                // 追加书籍信息
                HashMap<String, String> objectObjectHashMap = new HashMap<String, String>();
                objectObjectHashMap.put("bookName", RandomUtil.randomNumbers(100));
                searchBookResult.load(objectObjectHashMap);
            }
        }
    }

    public static interface SearchBookResult {

        void load(Map<String, String> bookInfo);

    }
}
